// 发表评论
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
      star:[{
          star:false
      },
      {
        star: false
      },
      {
        star: false
      },
      {
        star: false
      },
      {
        star: false
      }],
      stars:"",
      order_id:"",
      product:{},
      // 上传的图片列表
      photos:[],
      // 评价
      assess:"",
      picGather:[],
      submit:true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
  
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    wx.getStorage({
      key: 'order_id',
      success: function(res) {
        var order_id = res.data;
        wx.request({
          url: app.globalData.yuming + 'merchant.php/Api/ProductOrder/appraiseShow',
          data: {
            order_id:order_id
          },
          header: {
            "content-type": "application/x-www-form-urlencoded"
          },
          method: "POST",
          success: function (res) {
            var product = res.data.data.product;
            this.setData({
              product: product,
              order_id: order_id
            })
          }.bind(this),
          fail: function (res) {
            wx.showModal({
              title: '提示',
              content: res.data.message,
              showCancel: false
            })
          }
        })
      }.bind(this),
      fail:function(res){
        wx.showModal({
          title: '提示',
          content: res.data.message,
          showCancel:false
        })
      }
    })
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title: app.globalData.name,
      path: "/pages/shouye/shouye",
      imageUrl: app.globalData.headPic
    }
  },
  /**
   * 选星星
   */
  assess:function(e){
    var index = e.currentTarget.dataset.index;
    var star = this.data.star;
    star.map(function(val){
      val.star=false
    })
    for(var i=0;i<=index;i++){
      star[i].star =true;
    }
    this.setData({
      star:star,
      stars:index + 1
    })
    console.log(index)
  },
  /**
   * 获取评价
   */
  putAssess:function(e){
    var assess = e.detail.value;
    this.setData({
      assess: assess
    })
  },
  /**
   * 上传图片
   */
  photo:function(){
    var photos = this.data.photos;
    wx.chooseImage({
      count:5,
      success: function(res) {
        var photo = res.tempFilePaths;
        if(photos.length == 0){
          photos = photo
        }else{
          for (var i = 0, length = photo.length; i < length; i++) {
            var num = 0;
            for (var j = 0, l = photos.length; j < l; j++) {
              
              if (photo[i] != photos[j]) {
                num = num +1
              }
            }
            var len = photos.length;
            if(len >=5){
              
            }else{
              if (num == l) {
                photos.push(photo[i])
              }
            } 
          }
        }
        this.setData({
          photos:photos
        })
      }.bind(this),
      fail:function(res){
          console.log(res)
      }
    })
  },
  /**
   * 提交评价
   */
  submit:function(){
    var that = this;
    var order_id = this.data.order_id;
    var photos = this.data.photos;
    var picGather = [];
    var star = this.data.stars;
    var content = this.data.assess;
    var m_id = app.globalData.user.m_id;
      if (star == "") {
        wx.showModal({
          title: '提示',
          content: '请选择星级',
          showCancel: false
        })
      } else if(content == ""){
        wx.showModal({
          title: '提示',
          content: '请输入评价',
          showCancel: false
        })
      }else {
        this.setData({
          submit:false
        }) 
        wx.showLoading({
          title: '图片上传中',
        })
        if (photos.length >0){
          var length = photos.length;
          photos.map(function(val){
            wx.uploadFile({
              url: app.globalData.yuming + 'merchant.php/Api/ProductOrder/picUpload',
              filePath: val,
              name: 'pic',
              success: function (res) {
                var data = JSON.parse(res.data)
                picGather.push(data.data);
                console.log(picGather.join())
                if (picGather.length == photos.length) {
                  
                  wx.request({
                    url: app.globalData.yuming + 'merchant.php/Api/ProductOrder/addAppraise',
                    data:{
                      order_id: order_id,
                      picGather: picGather.join(),
                      star: star,
                      content: content,
                      m_id: m_id
                    },
                    header:{
                      "content-type":"application/x-www-form-urlencoded"
                    },
                    method:"POST",
                    success:function(res){
                      wx.setStorage({
                        key: 'order',
                        data: '4',
                        success:function(){
                          wx.redirectTo({
                            url: '/mine/myOrder/myOrder',
                          })
                        }
                      })
                    },
                    fail:function(res){
                      wx.hideLoading()
                        wx.showModal({
                          title: '提示',
                          content: res.data.message,
                          showCancel:false
                        })
                        this.setData({
                          submit: true
                        })
                    }.bind(this)
                  })
                }
              }.bind(this),
              fail: function (res) {

                wx: wx.showModal({
                  title: '提示',
                  content: res.data.message,
                  showCancel: false
                })
                this.setData({
                  submit: false
                })
              }.bind(this),
              complete: function (res) {
               
              }.bind(this)
            })
          }.bind(this))
        }else{
          wx.request({
            url: app.globalData.yuming + 'merchant.php/Api/ProductOrder/addAppraise',
            data: {
              order_id: order_id,
              picGather: "",
              star: star,
              content: content,
              m_id: m_id
            },
            header: {
              "content-type": "application/x-www-form-urlencoded"
            },
            method: "POST",
            success: function (res) {
              wx.setStorage({
                key: 'order',
                data: '4',
                success: function () {
                  wx.navigateTo({
                    url: '/mine/myOrder/myOrder',
                  })
                }
              })
            },
            fail: function (res) {
              wx.hideLoading()
              wx.showModal({
                title: '提示',
                content: res.data.message,
                showCancel: false
              })
              this.setData({
                submit: true
              })
            },
            complete: function (res) {
              wx.hideLoading()
            }
          })
        }
       
      }
  },
  /**
   * 删除图片
   */
  removeImg:function(e){
      var index = e.currentTarget.dataset.idx;
      var photos = this.data.photos;
      photos.splice(index,1)
      this.setData({
        photos:photos
      })
  }
})